package com.xxx;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.Function2;
import org.apache.spark.api.java.function.PairFunction;
import org.apache.spark.api.java.function.VoidFunction;
import scala.Tuple2;

import java.util.function.BiConsumer;

/**
 * @program: day0311
 * @description:
 * @author: CoreDao
 * @create: 2021-03-11 15:06
 **/

public class Pvuv2 {
    public static void main(String[] args) {

        SparkConf conf = new SparkConf().setMaster("local").setAppName("javawc");
        JavaSparkContext jsc = new JavaSparkContext(conf);

        JavaRDD<String> lineRDD = jsc.textFile("F:\\MyIDEA\\MyScala\\sxt\\day0311\\src\\main\\resources\\data\\pvuvdata");

        /*lineRDD.mapToPair(new PairFunction<String, String, Integer>() {
            @Override
            public Tuple2<String, Integer> call(String line) throws Exception {
                return new Tuple2<>(line.split("\t")[5] , 1);
            }
        }).reduceByKey(new Function2<Integer, Integer, Integer>() {
            @Override
            public Integer call(Integer v1, Integer v2) throws Exception {
                return v1 + v2;
            }
        }).foreach(new VoidFunction<Tuple2<String, Integer>>() {
            @Override
            public void call(Tuple2<String, Integer> stringIntegerTuple2) throws Exception {
                System.out.println(stringIntegerTuple2);
            }
        });
        */

        lineRDD.mapToPair(new PairFunction<String, String, String>() {
            @Override
            public Tuple2<String, String> call(String s) throws Exception {
                String[] str = s.split("\t");
                return new Tuple2<>(str[5],str[0]);
            }
        }).distinct().countByKey().forEach((x,y)->{
            System.out.println(x + " : " + y);
        });

        jsc.stop();

    }
}
